/****************************************************************************
 * Copyright (C) 2011-2013
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * MiiInfos.cpp
 *
 * for SaveGame Manager GX
 ***************************************************************************/
#include "MiiInfos.hpp"
#include "../Themes/CTheme.h"
#include "../Menu/Explorer.h"

MiiInfos::MiiInfos()
	: GuiFrame(0, 0)
{
	miiStruct = NULL;
	
	creatorIcon = Resources::GetImageData("icon_mii_creator.png");
	creatorImg = new GuiImage(creatorIcon);
	creatorImg->SetAlignment(ALIGN_LEFT | ALIGN_MIDDLE);
	
	dateIcon = Resources::GetImageData("icon_mii_date.png");
	dateImg = new GuiImage(dateIcon);
	dateImg->SetAlignment(ALIGN_LEFT | ALIGN_MIDDLE);
	
	genderIcon = NULL;
	genderImg = NULL;
	
	favColorImg = NULL;
	
	nameTxt = new GuiText("", 35, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	nameTxt->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	nameTxt->SetPosition(0, -55);
	nameTxt->SetMaxWidth(500, SCROLL_HORIZONTAL);
	Append(nameTxt);
	
	creatorTxt = new GuiText(L"...", 22, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	creatorTxt->SetAlignment(ALIGN_RIGHT | ALIGN_MIDDLE);
	creatorTxt->SetMaxWidth(350, SCROLL_HORIZONTAL);
	
	dateTxt = new GuiText(".. / ..", 22, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	dateTxt->SetAlignment(ALIGN_RIGHT | ALIGN_MIDDLE);
	dateTxt->SetMaxWidth(350, SCROLL_HORIZONTAL);
	
	frameCreator = NULL;
	frameDate = NULL;
}

MiiInfos::~MiiInfos()
{
	if(miiStruct)
		delete miiStruct;
	
	RemoveAll();
	
	if(frameCreator)
	{
		frameCreator->RemoveAll();
		delete frameCreator;
	}
	if(frameDate)
	{
		frameDate->RemoveAll();
		delete frameDate;
	}
	
	delete creatorImg;
	delete dateImg;
	Resources::Remove(creatorIcon);
	Resources::Remove(dateIcon);
	
	if(genderIcon)
		Resources::Remove(genderIcon);
	if(genderImg)
		delete genderImg;
	if(favColorImg)
		delete favColorImg;
	
	delete nameTxt;
	delete creatorTxt;
	delete dateTxt;
}

void MiiInfos::SetInfos(int index)
{
	if(miiStruct)
		delete miiStruct;
	miiStruct = new MiiStruct();
	
	if (Settings.LoadedBrowser == SDUSB_BROWSER)
	{
		std::string path = Settings.LastUsedPath;
		path += "/";
		path += CurrentBrowser()->GetItemName(index);
		miiStruct->name = CurrentBrowser()->GetItemDisplayName(index);
		MiiTools::GetDeviceInfos(path, miiStruct, true/*, false*/);
	}
	else
	{
		miiStruct->name = CurrentBrowser()->GetMiiName(index);
		miiStruct->creator = CurrentBrowser()->GetMiiCreator(index);
		miiStruct->position = 0;
		miiStruct->female = CurrentBrowser()->GetMiiFemale(index);
		miiStruct->month = CurrentBrowser()->GetMiiMonth(index);
		miiStruct->day = CurrentBrowser()->GetMiiDay(index);
		miiStruct->favColor = CurrentBrowser()->GetMiiFavColor(index);
	}
	
	nameTxt->SetText(GetName());
	creatorTxt->SetText(GetCreator());
	
	if(frameCreator)
	{
		frameCreator->RemoveAll();
		Remove(frameCreator);
		delete frameCreator;
	}
	frameCreator = new GuiFrame(creatorTxt->GetTextWidth()+creatorImg->GetWidth()+15, creatorImg->GetHeight());
	frameCreator->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	frameCreator->Append(creatorTxt);
	frameCreator->Append(creatorImg);
	
	dateTxt->SetText(GetDate());
	
	if(frameDate)
	{
		frameDate->RemoveAll();
		Remove(frameDate);
		delete frameDate;
	}
	frameDate = new GuiFrame(dateTxt->GetTextWidth()+dateImg->GetWidth()+15, dateImg->GetHeight());
	frameDate->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	frameDate->Append(dateTxt);
	frameDate->Append(dateImg);
	
	if(genderImg)
	{
		Remove(genderImg);
		delete genderImg;
		Resources::Remove(genderIcon);
	}
	genderIcon = Resources::GetImageData(miiStruct->female ? "icon_mii_female.png" : "icon_mii_male.png");
	genderImg = new GuiImage(genderIcon);
	genderImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	
	if(favColorImg)
	{
		Remove(favColorImg);
		delete favColorImg;
	}
	favColorImg = new GuiImage(24, 24, GetFavColor());
	favColorImg->SetAlignment(ALIGN_RIGHT | ALIGN_BOTTOM);
	
	int x = ((frameDate->GetWidth() > frameCreator->GetWidth()) ? frameDate->GetWidth() : frameCreator->GetWidth()) + 50 + 24;
	int y = frameDate->GetHeight() + frameCreator->GetHeight() + 10;
	this->SetSize(x, y);
	
	Append(genderImg);
	Append(favColorImg);
	Append(frameCreator);
	Append(frameDate);
}

std::wstring MiiInfos::GetName()
{
	std::wstring name = miiStruct->name;
	if(name.empty())
		name =  L"...";
	
	return name;
}

std::wstring MiiInfos::GetCreator()
{
	std::wstring creator = miiStruct->creator;
	if(creator.empty())
		creator =  L"...";
	
	return creator;
}

std::string MiiInfos::GetDate()
{
	std::string date = "..";
	
	if(miiStruct->day != 0)
		date = fmt("%d", miiStruct->day);
	date += " / ";
	
	switch(miiStruct->month)
	{
		case 0: { date += ".."; break; }
		case 1: { date += tr("January"); break; }
		case 2: { date += tr("February"); break; }
		case 3: { date += tr("March"); break; }
		case 4: { date += tr("April"); break; }
		case 5: { date += tr("May"); break; }
		case 6: { date += tr("June"); break; }
		case 7: { date += tr("July"); break; }
		case 8: { date += tr("August"); break; }
		case 9: { date += tr("September"); break; }
		case 10: { date += tr("October"); break; }
		case 11: { date += tr("November"); break; }
		case 12: { date += tr("December"); break; }
	}
	return date;
}

GXColor MiiInfos::GetFavColor()
{
	GXColor color = (GXColor) {0, 0, 0, 0};
	
	switch (miiStruct->favColor)
	{
		case 0: { color = (GXColor) {255, 0, 0, 255}; break; }
		case 1: { color = (GXColor) {255, 150, 0, 255}; break; }
		case 2: { color = (GXColor) {255, 255, 0, 255}; break; }
		case 3: { color = (GXColor) {140, 255, 0, 255}; break; }
		case 4: { color = (GXColor) {0, 130, 0, 255}; break; }
		case 5: { color = (GXColor) {0, 100, 255, 255}; break; }
		case 6: { color = (GXColor) {0, 200, 255, 255}; break; }
		case 7: { color = (GXColor) {255, 50, 140, 255}; break; }
		case 8: { color = (GXColor) {190, 0, 255, 255}; break; }
		case 9: { color = (GXColor) {140, 80, 45, 255}; break; }
		case 10: { color = (GXColor) {255, 255, 255, 255}; break; }
		case 11: { color = (GXColor) {0, 0, 0, 255}; break; }
	}
	
	return color;
}
